package cn.HL.pet.service.impl;

import cn.HL.basic.util.BaiduAuditUtils;
import cn.HL.org.domain.Employee;
import cn.HL.org.mapper.EmployeeMapper;
import cn.HL.pet.domain.*;
import cn.HL.pet.mapper.*;
import cn.HL.pet.service.IProductService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import cn.HL.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-30
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            StringBuffer sb = new StringBuffer();
            //审核名字
            if(!BaiduAuditUtils.TextCensor(product.getName())){
                sb.append(product.getName()+"名字不合法!!");
            }
            //审核照片
            if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+product.getResources().split(",")[0])){
                sb.append(product.getName()+"照片不合法!!");
            }
            /*//审核不通过
            if(sb.toString().length()>0){
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(product.getId());
                petOnlineAuditLog.setNote(sb.toString());
                //记录日志
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
            }*/
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("onSaleTime",new Date());
            productMapper.onSale(map);


        }

    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        productMapper.offSale(map);
    }

    @Override
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        if(detail!=null){
            productDetailMapper.save(detail);
        }
    }

    @Override
    public void removeById(Long id) {
        ProductDetail detail =  productDetailMapper.loadByProductId(id);
        productMapper.deleteById(id);
        if(detail!=null){
            productDetailMapper.deleteById(detail.getId());
        }

    }
}
